package redis

import (
	"fmt"
	"github.com/go-redis/redis"
	"shoppingcart-srv/appconfig"
	"sync"
)

var (
	redisClient *redis.Client
	err         error
	once        sync.Once
)

func InitRedis() {
	host := appconfig.AppConf1.RedisConfig.Host
	name := appconfig.AppConf1.RedisConfig.DBName
	password := appconfig.AppConf1.RedisConfig.Password
	port := appconfig.AppConf1.RedisConfig.Port
	addr := fmt.Sprintf("%s:%d", host, port)
	redisClient = redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: password,  // no password set
		DB:       int(name), // use default DB
	})

	pong, err := redisClient.Ping().Result()
	fmt.Println(pong, err)
	if err != nil {
		fmt.Println("redis连接失败")
	}
}
